package common.server.handler.inside;

import common.server.entity.InsideResponseMessage;
import common.server.exception.BaseException;
import common.server.respcode.RespCodeService;
import common.server.respcode.RespHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

/**
 * 全局rest请求异常拦截器
 * @author Administrator
 * @version [版本号, 2017/11/15 0015]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@ControllerAdvice(annotations = {RestController.class})
public class RestExceptionHandler  {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(RestExceptionHandler.class);


    /**
     * 构建异常返回
     * @param e
     * @param <T>
     * @return
     */
    @ExceptionHandler
    @ResponseBody
    private <T> InsideResponseMessage<T> runtimeExceptionHandler(Exception e) {
        logger.error("---------> runtimeExceptionHandler error!", e);
        return RespHelper.buildInsideRespMessage(RespCodeService.COMM_ERROR,null);
    }

    @ExceptionHandler(BaseException.class)
    @ResponseBody
    private <T> InsideResponseMessage<T> BaseExceptionHandler(BaseException e) {
        logger.error("---------> BaseExceptionHandler error!", e);
        return RespHelper.buildInsideRespMessage(e);
    }


    /**
     * 构建异常返回
     * @param e
     * @param <T>
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    private <T> InsideResponseMessage<T> illegalParamsExceptionHandler(MethodArgumentNotValidException e) {
        logger.error("---------> illegalParamsExceptionHandler request!", e);
        return RespHelper.buildInsideRespMessage(RespCodeService.COMM_ERROR,null);
    }
}
